<?
/**
* Copyright (c) 2002-2007 BioWare Corp.
* http://www.bigdaddymonitor.org
*
* This file is part of the BigDaddy Monitoring
* program. It is free software distributed under
* the terms of the BSD License. It is ditributed
* in the hopes that it will be useful, but
* WITHOUT ANY WARRANTY; without event the implied
* warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE.
*
* The BSD License is available from:
* http://www.opensource.org/licenses/bsd-license.php
**/
/** -------------------------------------------------------------------------------------------
* DBConfig Class for BIGDADDY
* @author Duleepa Wijayawardhana <dups@bioware.com>
* created 23 February 2004
* last modified 19 April 2006
*
* This Class provides all the database connections to all the appropriate database
* handles. This file should be encrypted so that it can only be read by a few people, access
* to the contents of this file will give you the database passwords and or worse access into
* the database itself
*
* 
*/

class DatabaseHandle
{
    /**
     * @var array Containing the sections to fetch from the DBConfigINI
     */
    private $DBsections;
    /**
     * @var array CContaining all the connection info
     */
    private $DBInfo;
    /**
     * @var string Contains the DB group to get info for
     */
    public $DBGrouping;



    /**---------------------------------------------------------------------------------------
    * function __construct
    * @param $DBGrouping string containing the grouping for the database, "community", "internal", "bigdaddy"
    * @return void
    * @author Duleepa Wijayawardhana <dups@bioware.com>
    * @version C 02.23.04 M 07.12.05
    * */
    function __construct($DBGrouping)
    {
        $this->DBGrouping=$DBGrouping;
        
        //These groupings contain the array of database handles that are allowed to be fetched
        //for each grouping. Each grouping is then changed depending on which database scope
        switch ($this->DBGrouping)
        {
            //Bigdaddy site
            case "bigdaddy":
                $this->DBsections=array(
					"bigdaddy",
					"bigdaddystats",
                );
            break;
        }

        if (!is_array($this->DBInfo))
        {
            $this->ReadDBConfig();
        }
    }

    /**---------------------------------------------------------------------------------------
    * function ReadDBConfig
    * Reads the database confid and populates $this->DBInfo
    * @return void
    * @author Duleepa Wijayawardhana <dups@bioware.com>
    * @version C 02.23.04 M 09.20.05
    * */
    private function ReadDBConfig()
    {
        //Database passwords
        require("/bigdaddy/bigdaddy_dbconfig_php.inc");

        //Let's grab the areas we want.
        $numSections=count($this->DBsections);
        for ($i=0; $i<$numSections; $i++)
        {
            $databaseInfo[$this->DBsections[$i]]["pass"]=$aDBInfo[$this->DBsections[$i]]["pass"];
            $databaseInfo[$this->DBsections[$i]]["user"]=$aDBInfo[$this->DBsections[$i]]["user"];
            $databaseInfo[$this->DBsections[$i]]["database"]=$aDBInfo[$this->DBsections[$i]]["database"];
            $databaseInfo[$this->DBsections[$i]]["host"]=$aDBInfo[$this->DBsections[$i]]["host"];
        }

        $this->DBInfo=$databaseInfo;
    }

    /**---------------------------------------------------------------------------------------
    * function GetDBHandle
    * creates a database handle with mysql connect. Use constant USE_NONPERSISTANT_DBCONNECTION
    * to set whether the database connection is persistant or not
    * @param string $dbhost Database host
    * @param string $dbname Database name
    * @param string $dbuser Database user
    * @param string $dbpasswd Database password
    * @return dbhandle
    * @author Duleepa Wijayawardhana <dups@bioware.com>
    * @version C 02.23.04 
    * */
    private function GetDBHandle($dbhost, $dbname, $dbuser, $dbpasswd)
    {
        for ($i=0; $i<2; $i++)
        {
                if (USE_NONPERSISTANT_DBCONNECTION==1)
                {
                    $dbh = mysql_connect($dbhost, $dbuser, $dbpasswd);
                }
                else
                {
                    $dbh = mysql_pconnect($dbhost, $dbuser, $dbpasswd);
                }
    
            if ($dbh==false)
            {
                sleep(1);
            }
            else
            {
                break;
            }
        }

        if ($dbh==false)
        {
            return false;
        }
    
        @mysql_select_db($dbname, $dbh);
        return $dbh;
    }
    
    /**---------------------------------------------------------------------------------------
    * function FindDBHandle
    * Finds the correct Database Handle and connection to get a handle for
    * @param string $sPointer Pointer for the handle
    * @return string 
    * @author Duleepa Wijayawardhana <dups@bioware.com>
    * @version C 07.12.05 
    * */
    public function FindDBHandle($sPointer)
    {
        switch ($sPointer)
        {
            case "bigdaddy":
                return "bigdaddy";
            case "monitor":
                return "bigdaddy";
	    case "bigdaddystats":
                return "bigdaddystats";
        }
    }

    /**---------------------------------------------------------------------------------------
    * function GetSpecificHandle
    * Finds the correct Database Handle and connection to get a handle for
    * @param string $sPointer Pointer for the handle
    * @return dbhandle Database handle
    * @author Duleepa Wijayawardhana <dups@bioware.com>
    * @version C 07.12.05 
    * */
    public function GetSpecificHandle($sPointer)
    {
        //Find the handle for this Pointer
        $sHandle = $this->FindDBHandle($sPointer);
        
        if ($sHandle != "")
        {
            return $this->GetDBHandle(
                $this->DBInfo[$sHandle]['host'],
                $this->DBInfo[$sHandle]['database'],
                $this->DBInfo[$sHandle]['user'],
                $this->DBInfo[$sHandle]['pass']);
        }
    }

    /**---------------------------------------------------------------------------------------
    * function RunSystemMYSQLCommand
    * Runs a MYSQL Command on the system line
    * @param string $sPointer Database pointer
    * @param string $sMySQLCMD Command to run on the mysql command line
    * @param bool $DEBUG set to 1 to simply return the command
    * @return mixed last line of system output / string command if debug / false on failure
    * @author Duleepa Wijayawardhana <dups@bioware.com>
    * @version C 07.12.05
    * */
    public function RunSystemMYSQLCommand($sPointer, $sMySQLCMD, $DEBUG=NULL )
    {
        if ($sMySQLCMD != "")
        {
            //Find the handle for this Pointer
            $sHandle = $this->FindDBHandle($sPointer);
            
            if ($sHandle != "")
            {
                $sPassword = $this->decrypt_text($this->DBInfo[$sHandle]['pass']);
                
                $sCMD = "mysql --user=".$this->DBInfo[$sHandle]['user']." --password=".$sPassword." --host=".$this->DBInfo[$sHandle]['host']." ".$this->DBInfo[$sHandle]['database']." ".$sMySQLCMD;
                //This is to remove some weird high ASCII character issue
                $sCMD = preg_replace("/[^\x20-\x7E]/", "", $sCMD);
                if ($DEBUG!=1)
                {
                    $sLastLine = system($sCMD);
                }
                else
                {
                    return $sCMD;
                }
            }
            else
            {
                return false;
            }
        }
        
        return $sLastLine;
    }
}



?>
